package com.fengwk.user.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fengwk.support.code.Sequence;
import com.fengwk.support.exception.BaseException;
import com.fengwk.support.util.CollectionUtils;
import com.fengwk.support.util.ObjectUtils;
import com.fengwk.user.dao.UsChannelDao;
import com.fengwk.user.face.entity.UsChannel;
import com.fengwk.user.face.model.Channel;
import com.fengwk.user.service.ChannelService;

@Service
public class ChannelServiceImpl implements ChannelService {

	private static Map<String, Channel> CHANNEL_MODEL_MAP;
	
	@Autowired
	private UsChannelDao usChannelDao;
	
	@PostConstruct
	public void init() {
		buildChannelModelMap();
	}
	
	private void buildChannelModelMap() {
		List<UsChannel> usChannelList = usChannelDao.queryAll();
		// 只进行读取操作,这里采用LinkedHashMap
		CHANNEL_MODEL_MAP = CollectionUtils.toMapWithTypeConvert(
				usChannelList, 
				usChannel -> usChannel.getChannel(), 
				usChannel -> usChannelToChannel(usChannel));
	}
	
	@Override
	public int getStrategyByChannel(String channel) {
		Channel channelM = getChannel(channel);
		if (channelM != null) {
			return channelM.getStrategy();
		}
		throw new BaseException("通道获取异常");
	}
	
	@Override
	public Channel saveChannel(String channel, Integer strategy, String description) {
		UsChannel usChannel = new UsChannel();
		usChannel.setRecordId(Sequence.next());
		usChannel.setChannel(channel);
		usChannel.setStrategy(strategy);
		usChannel.setDescription(description);
		usChannelDao.insert(usChannel);
		return usChannelToChannel(usChannel);
	}

	@Override
	public Channel removeChannel(String channel) {
		UsChannel usChannel = usChannelDao.findByChannel(channel);
		if (usChannel != null) {
			usChannelDao.deleteByChannel(channel);
		}
		return usChannelToChannel(usChannel);
	}

	@Override
	public List<Channel> listChannel() {
		// 刷新通道
		buildChannelModelMap();
		// 构建
		List<Channel> channelList = new ArrayList<>();
		CHANNEL_MODEL_MAP.forEach((k, v) -> {
			channelList.add(ObjectUtils.clone(v));
		});
		return channelList;
	}
	
	private Channel usChannelToChannel(UsChannel usChannel) {
		Channel channel = null;
		if (usChannel != null) {
			channel = new Channel();
			channel.setChannel(usChannel.getChannel());
			channel.setStrategy(usChannel.getStrategy());
			channel.setDescription(usChannel.getDescription());
		}
		return channel;
	}
	
	private Channel getChannel(String channel) {
		Channel channelM = CHANNEL_MODEL_MAP.get(channel);
		if (channelM != null) {
			UsChannel usChannel = usChannelDao.findByChannel(channel);
			if (usChannel == null)
				throw new BaseException("通道[" + channel + "]不存在");
			buildChannelModelMap();
			channelM = CHANNEL_MODEL_MAP.get(channel);
		}
		return channelM;
	}

}
